package com.jsptpd.smartauth.modules.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jsptpd.smartauth.modules.business.dao.db1.UserMapper;
import com.jsptpd.smartauth.modules.business.model.User;
import com.jsptpd.smartauth.modules.business.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService
{

    @Autowired
    UserMapper userMapper;

    /**
     * 从t_user里获取用户信息
     */
    @Override
    public User findByUserId(String userId) {
        User user = selectOne(new EntityWrapper<User>().eq("USER_ID", userId));
        if(user!=null){
            return user;
        }
        return null;
    }

    /**
     * 从t_user里获取用户信息:user_id,employ_id,email?
     */
    @Override
    public User findByUserIdPlus(String userId) {
        User user = selectOne(new EntityWrapper<User>()
                .where("USER_ID={0} OR EMPLOY_ID={0}", userId));
        if(user!=null){
            return user;
        }
        return null;
    }

    /**
     * 从t_user以及t_organization里获取用户信息以及部门信息
     */
    @Override
    public User findAllByUserId(String id) {
        User user = userMapper.findAllByUserId(id);
        if(user!=null){
            return user;
        }
        return null;
    }

    @Override
    public User findAllByEmployeeId(String id) {
        User user = userMapper.findAllByEmployeeId(id);
        if(user!=null){
            return user;
        }
        return null;
    }

    @Override
    public User findAllByHrId(String id) {
        User user = userMapper.findAllByHrId(id);
        if(user!=null){
            return user;
        }
        return null;
    }

    /**
     * 更新用户信息
     */
    @Override
    public boolean updateUser(User user) {
        if(user!=null){
            return insertOrUpdate(user);
        }else{
            return false;
        }

    }

    /*
     * 检查用户状态是否有效：是否过期
     */
    @Override
    public boolean validate(String userId) {
        User user = findByUserId(userId);

        if(user==null){
            return false;
        }

        //暂时放过
        if(user.getExpirationDate()==null){
            return true;
        }

        if((new Date().getTime()) < user.getExpirationDate().getTime()){
            return true;
        }

        return false;
    }
}
